<template>
  <div style="padding: 10px 15px">
    <!-- 查询条件-->
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="请输入问题名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
        >新增</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查询</el-button
      >
    </div>
    <!--查询条件 end-->

    <!--table-->
    <el-table
      v-loading="listLoading"
      :data="CommProblemData"
      border
      fit
      highlight-current-row
      style="width: 100%"
      stripe
    >
      <el-table-column label="问题名称" align="left" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="left" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="left" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.uptDtt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="left" min-width="150px">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.stt == 1"
            type="text"
            title="状态"
            :style="{ color: 'green' }"
            circle
            @click.stop="() => IsShow(row)"
            >显示</el-button
          >
          <el-button
            v-else
            type="text"
            title="状态"
            :style="{ color: 'red' }"
            circle
            @click.stop="() => IsShow(row)"
            >隐藏</el-button
          >
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleClick(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" @click="handleClick(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="listQuery.total > 0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData(this)"
    />

    <!--table end -->

    <!--弹出层  新增-->
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      width="55%"
      :before-close="handleClose"
      :show-close="false"
    >
      <el-form :model="form" :inline="false" label-position="right">
        <el-form-item label="问题名称">
          <el-input v-model="form.name" style="width: 300px" />
        </el-form-item>
        <el-form-item label="内容">
          <Tinymce ref="editor" v-model="form.content" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveForm">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Tinymce from "@/components/Tinymce";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { AjaxToken, TipConfirm } from "@/api/common";
import { SelectAjax } from "@/api/common";
import { Jurisdiction } from "@/api/common";
import sessionStorage from "@/api/Storage";
import "url-search-params-polyfill"; // 兼容IE，否则无法识别URLSearchParams
export default {
  name: "CommProblem",
  components: { Tinymce, Pagination },
  data() {
    return {
      logincpnid: "",
      multipleSelection: [],
      paytypeoptions: [],
      formLabelWidth: "120px",
      selectpaytypes: [],
      CommProblemData: null,
      IsAdd: 1,
      xlcontent: [],
      listQuery: {
        name: "",
        total: 0,
        page: 1,
        limit: 10,
      },
      listLoading: true,
      dialogFormVisible: false,
      dialogdetailsVisible: false,
      paytypeVisible: false,
      dialogStatus: "",
      textMap: {
        update: "修改",
        create: "添加",
      },
      form: {
        id: "",
        name: "",
        content: "",
        brf: "",
        stt: 1,
        isDelete: "N",
        uptDtt: "",
      },
    };
  },

  // 初始化
  created() {
    var userinfo = JSON.parse(sessionStorage.getItem("userinfo"));
    this.logincpnid = userinfo.cpnId;
    this.fetchData();
  },
  methods: {
    // 弹层关闭
    handleClose() {},
    // 权限
    Jurisdiction(moduleActionName) {
      return Jurisdiction(moduleActionName, this);
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 多选赋值
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    // 搜索
    handleFilter() {
      this.listQuery.page = 1;
      this.fetchData();
    },
    // 新增
    handleCreate() {
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.form.id = "";
      this.form.name = "";
      this.form.content = "";
    },
    // 编辑
    handleClick(row) {
      this.IsAdd = 0;
      this.form = row;
      //   this.form.name = row.name;
      //   this.form.content = row.content;
      this.dialogStatus = "edit";
      this.dialogFormVisible = true;
      //   var params = new URLSearchParams();
      //   params.append("ID", this.form.id);
      //   backstage.GetCommProblem(this, params);
    },
    //隐藏或者显示
    IsShow(row) {
      row.stt = row.stt == 0 ? 1 : 0;
      var params = new URLSearchParams();
      params.append("str", JSON.stringify(row));
      this.listLoading = true;
      backstage.SaveCommProblem(this, params);
    },
    // 删除
    deleteClick(row) {
      row.isDelete = "Y";
      var params = new URLSearchParams();
      params.append("str", JSON.stringify(row));
      this.$confirm("确认删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then((a) => {
          this.listLoading = true;
          backstage.SaveCommProblem(this, params);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
          this.listLoading = false;
        });
    },
    // 保存
    saveForm() {
      var params = new URLSearchParams();
      params.append("str", JSON.stringify(this.form));
      backstage.SaveCommProblem(this, params);
    },

    // 查询
    fetchData() {
      this.listLoading = true;
      var params = new URLSearchParams();
      params.append(
        "Parms",
        '{"page":' +
          this.listQuery.page +
          ',"rows":' +
          this.listQuery.limit +
          "}"
      );
      var user = JSON.parse(sessionStorage.getItem("userinfo"));
      params.append("Name", this.listQuery.name);
      backstage.GetCommProblemLst(this, params);
    },
  },
};
// 后台事件
var backstage = {
  // 获取CommProblem集合
  GetCommProblemLst: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "GetCommProblemByPage",
      params,
      "post",
      function (res) {
        thisobj.listLoading = false;
        var body = res.data.data;
        body.rows.forEach((element) => {
          element.uptDtt = element.uptDtt.replace("T", " ");
        });
        thisobj.CommProblemData = [];
        thisobj.CommProblemData = body.rows;

        thisobj.listQuery.page = body.page;
        thisobj.listQuery.total = body.records;
      },
      function (err) {
        thisobj.listLoading = false;
      }
    );
  },
  GetCommProblem: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "GetCommProblem",
      params,
      "post",
      function (res) {
        
        var body = res.data;
        if (body.success) {
          thisobj.form = body.data;
          thisobj.$refs.editor.setContent(thisobj.form.content || "");
        }
        thisobj.$message({
          showClose: true,
          message: body.message,
          type: body.success ? "success" : "error",
        });
      },
      function (err) {}
    );
  },
  SaveCommProblem: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "SaveCommProblem",
      params,
      "post",
      function (res) {
        var body = res.data;

        if (body.success) {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "success",
            duration: 2000,
          });
          thisobj.fetchData();
          thisobj.dialogFormVisible = false;
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "error",
          });
        }
      },
      function (err) {}
    );
  },
};
</script>

